<template>
  <div class="match-page">
    <div class="left-section">
      <div id="player" ref="player" class="player">
        <iframe
          v-if="iframeVisible && matches.length > 0"
          id="myIframe"
          class="player"
          :key="matches[select]?.id"
          src="https://video.dunduiby.com/?isLive=1&isRobot=0&isLogined=0&isHideCtrl=0&isNeedDanmaku=0&volume=1&isForceHls=0&isTwitch=0&isNeedTheaterMode=1&isNeedWaterMask=false&waterMaskImg=jingyu&isPIP=false"
        ></iframe>
      </div>

      <div class="join-live" @click="joinLive">进入直播间</div>
    </div>
    <div class="right-section">
      <div
        class="match-card-container"
        v-for="(match, index) in matches"
        :key="match.id"
        :class="select === index && 'active'"
      >
        <div
          class="match-card"
          @click="() => selectHandler(index)"
          :class="select === index && 'active'"
        >
          <!-- <div class="match-header">
          <span class="match-league">{{ match.leagueName }}</span>
        </div> -->
          <!-- <div class="match-info">
          <div class="teams active">
            <div class="team">{{ match.hostName }}</div>
            <div class="score">
              <img :src="match.hostTeamLogo" />{{ match.hostTeamScore }} - {{ match.guestTeamScore
              }}<img :src="match.guestTeamLogo" />
            </div>
            <div class="team">{{ match.guestName }}</div>
          </div>
          <div class="time">{{ match?.statusLable }}</div>
        </div> -->
          <img :src="match.coverImage" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, nextTick } from 'vue'
// import DPlayer from 'dplayer'
import { getHomeBannerList } from '@/api/home'
import { useRouter } from 'vue-router'
// import Player from 'xgplayer'
// import 'xgplayer/dist/index.min.css'
// import HlsJsPlugin from 'xgplayer-hls.js'
// import FlvJsPlugin from 'xgplayer-flv.js'
const player = ref(null)
const select = ref(0)
const matches = ref([])
const hasMute = ref(true)
const router = useRouter()
const iframeVisible = ref(true)
let interval = null
// const iframeUrl = ref('')

const coverImages = [
  'https://sta-prod-pic.codlupp.com/avatar/p0020201026191328030063.png',
  'https://sta-prod-pic.codlupp.com/avatar/p0020240807114504087278.jpg',
  'https://sta-prod-pic.codlupp.com/avatar/p0020201112153416090558.png',
  'https://sta-prod-pic.codlupp.com/avatar/p0020240831203130099147.png',
  'https://sta-prod-pic.codlupp.com/avatar/p0020240807193831022935.jpg'
]

onMounted(() => {
  getData()
})

const getData = async () => {
  try {
    const res = await getHomeBannerList()
    console.log(res, 'res')
    if (res.code === 200) {
      matches.value = res.data
        .map((item, index) => ({
          ...item,
          coverImage: coverImages[index % coverImages.length]
        }))
        .slice(0, 5)
      initPlayer(matches.value[0]?.playAddr)
    }
  } catch (error) {
    console.log(error, 'error')
  }
}

const selectHandler = (index) => {
  try {
    select.value = index
    iframeVisible.value = false
    nextTick(() => {
      iframeVisible.value = true
      setTimeout(() => {
        sendMessage(matches.value[index]?.playAddr)
      }, 1000)
    })
  } catch (error) {
    console.log(error, 'error')
  }
}
window.addEventListener('message', function (event) {
  if (event.data.msg === 'playing') {
    if (interval) {
      clearInterval(interval)
      interval = null
    }
  }
})
// const initIframe = () => {}
function sendMessage(playAddr) {
  try {
    const iframe = document.getElementById('myIframe')
    const data = {
      playAddr: {
        flv: playAddr.ori_flv,
        m3u8: playAddr.ori_m3u8,
        DPlayerID: 'iFrame_Player'
      }
    }
    if (interval) {
      clearInterval(interval)
      interval = null
    }
    console.log(iframe, 'iframe')
    iframe.contentWindow?.postMessage(data, '*')
    // 使用 postMessage 向 iframe 发送消息
    interval = setInterval(() => {
      console.log(data, 'data')
      iframe.contentWindow?.postMessage(data, '*')
    }, 1000)
  } catch (error) {
    console.log('Failed to send message on the first attempt:', error)
  }
}
const initPlayer = (playAddr) => {
  nextTick(() => {
    const iframe = document.getElementById('myIframe')
    const data = {
      playAddr: {
        flv: playAddr.ori_flv,
        m3u8: playAddr.ori_m3u8,
        DPlayerID: 'iFrame_Player',
        volume: 1,
      }
    }
    if (interval) {
      clearInterval(interval)
      interval = null
    }
    console.log(iframe, 'iframe')
    iframe.contentWindow?.postMessage(data, '*')
    // 使用 postMessage 向 iframe 发送消息
    interval = setInterval(() => {
      console.log(data, 'data')
      iframe.contentWindow?.postMessage(data, '*')
    }, 1000)
    // iframe.addEventListener('load', () => {
    //   // console.log(iframe.contentWindow.document)
    //   // iframe.contentWindow.document.querySelectorAll('video')[0].style = 'object-fit:cover'
    //   // document.querySelectorAll('video')[0].style = 'object-fit:cover'
    // })
  })
}

const joinLive = () => {
  router.push(`/live/${matches.value[select.value].anchorId}`)
}

const cancelMute = () => {
  try {
    console.log(player.value, 'player.value')
    player.value.volume = 1
    player.value.setConfig({
      volume: 1
    })
    hasMute.value = false
  } catch (error) {
    console.log(error, 'error')
  }
}
onUnmounted(() => {
  window.removeEventListener('message', function (event) {})
  if (interval) {
    clearInterval(interval)
    interval = null
  }
})
</script>

<style lang="scss" scoped>
.match-page {
  display: flex;
  justify-content: space-between;
  //   max-width: 1400px;
  margin: 0 auto;
  //   padding: 20px;
  //   background-color: #f5f5f5;
  height: 616px;
  background: linear-gradient(180deg, #e5eeff, #e8f0ff);
  border: 1px solid hsla(0, 0%, 100%, 0.6);
  box-shadow: 0 10px 20px 0 rgba(91, 102, 139, 0.2);
  overflow: hidden;
  border-radius: 8px;
  .left-section {
    // width: 70%;
    background-color: #000;

    overflow: hidden;
    width: 1170px;
    position: relative;
    // box-shadow: 0px 0px 10px rgba($color: #000000, $alpha: 0.3);
    &:hover {
      .join-live {
        opacity: 1;
      }
    }
    .player {
      width: 100%;
      height: 100%;
      border: none;
      outline: none;
    }
    .join-live {
      position: absolute;
      width: 180px;
      height: 60px;
      left: 50%;
      top: 50%;
      margin-left: -95px;
      margin-top: -30px;
      background-size: 100% 100%;
      background-repeat: no-repeat;
      background-image: url('@/assets/icons/home_live_join_liv_btn.webp');
      font-size: 20px;
      font-weight: 600;
      color: #fff;
      z-index: 9;
      display: flex;
      align-items: center;
      justify-content: center;
      // padding-bottom: 5px;
      opacity: 0;
      cursor: pointer;
      line-height: 60px;
    }
    .mute-tips {
      position: absolute;
      bottom: 50px;
      left: 50%;
      z-index: 4;
      transform: translateX(-50%);
      width: 184px;
      height: 62px;
      border-radius: 4px;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      background: #ff5d23;
      background-size: 100% 100%;
      color: #fff;

      // display: block;
      font-size: 16px;
      text-align: center;
      color: #fff;
      margin-left: 2px;
      .icon {
        width: 22px;
      }
    }
  }

  .right-section {
    // width: 28%;
    display: flex;
    flex-direction: column;
    // gap: 10px;
    flex: 1;
    padding-left: 14px;
    padding-top: 6px;
    .match-card-container {
      width: 204px;
      height: 112px;
      border: 3px solid transparent;
      position: relative; // 添加这行
      // border-radius: 10px;
      margin-bottom: 10px;

      &::before {
        content: '';
        position: absolute;
        left: -10px; // 调整箭头位置
        top: 50%;
        transform: translateY(-50%);
        width: 0;
        height: 0;
        border-top: 10px solid transparent;
        border-bottom: 10px solid transparent;
        border-right: 10px solid #16b13a; // 箭头颜色
        opacity: 0; // 默认不可见
        // transition: opacity 0.3s ease;
      }

      &.active::before,
      &:hover::before {
        opacity: 1; // 当激活或悬停时显示箭头
      }
      .match-card {
        // background: url('@/assets/icons/home_live_bg.webp') no-repeat center center;
        // background-size: 100% 100%;
        border-radius: 10px;
        overflow: hidden;
        //   padding: 10px;
        width: 204px;
        height: 112px;
        display: flex;
        align-content: center;
        justify-content: center;
        cursor: pointer;
        // flex-direction: column;
        align-items: center;
        &.active {
          background-size: 100% 100%;
          border: 3px solid #16b13a;
          // box-shadow: 0px 0px 10px rgba(31, 200, 59, 1);
        }
        &:hover {
          // background-size: 100% 100%;
          border: 3px solid #16b13a;
          // box-shadow: 0px 0px 10px rgba(31, 200, 59, 0.5);
        }
        // box-shadow: 0px 0px 7px rgba($color: #000000, $alpha: 0.3);

        // transition: all 0.5s;

        img {
          width: 200px;
          height: 108px;
          object-fit: fill;
          border-radius: 5px;
        }
        // .match-header {
        //   display: flex;
        //   align-items: center;
        //   background: url('@/assets/icons/home_live_title_bg.webp') no-repeat center center;
        //   background-size: 100% 100%;
        //   width: 184px;
        //   height: 24px;
        //   text-align: center;
        //   line-height: 24px;
        //   display: flex;
        //   //   align-content: center;
        //   justify-content: center;
        //   // flex-direction: column;
        //   align-items: center;
        //   // padding: 5px 10px;
        //   // border-top-left-radius: 8px;
        //   // border-top-right-radius: 8px;
        //   font-size: 14px;
        //   img {
        //     display: none; /* Hide the img element, as background is set via CSS */
        //   }

        //   .match-league {
        //     font-weight: bold;
        //     color: white;
        //   }
        // }

        // .match-info {
        //   // padding: 10px;
        //   // flex: 1;
        //   padding-top: 10px;
        //   .teams {
        //     display: flex;
        //     justify-content: center;
        //     align-items: center;
        //     margin-bottom: 5px;
        //     color: #525a79;

        //     .team {
        //       font-weight: bold;
        //       font-weight: 400;
        //       font-size: 14px;

        //       text-overflow: ellipsis;
        //       overflow: hidden;
        //       white-space: nowrap;
        //       width: 70px;

        //       &:nth-child(1) {
        //         text-align: right;
        //       }
        //       &:nth-child(2) {
        //         text-align: left;
        //       }
        //     }

        //     .score {
        //       // color: #333;
        //       font-weight: 400;
        //       font-size: 14px;
        //       width: 120px;
        //       text-align: center;
        //       // margin: 0 5px;
        //       display: flex;
        //       align-items: center;
        //       justify-content: center;

        //       img {
        //         height: 20px;
        //         width: 20px;
        //         object-fit: cover;
        //         margin: 0 5px;
        //       }
        //     }
        //     &.active {
        //       font-weight: 400;
        //       font-size: 14px;
        //       // color: #16b13a;
        //     }
        //   }

        //   .time {
        //     font-family:
        //       PingFang SC,
        //       PingFang SC;
        //     font-weight: 400;
        //     font-size: 13px;
        //     color: #e95d5d;
        //     text-align: center;
        //   }
        // }
      }
    }
  }
}
</style>

<style>
.player {
}
video {
  object-fit: cover;
}
</style>
